package ObstacleTest;

import java.util.ArrayList;

import lejos.robotics.navigation.WayPoint;
import Movement.NEPilot;
import NursE.NEHead;
import NursE.NENurse;

public class ThreadObstacle extends Thread{
	
	protected NENurse nurse;
	protected NEHead head;
	protected NEPilot pilot;
	protected boolean running = true;
	
	
	public ThreadObstacle(NENurse nurse){
		this.nurse=nurse;
		this.head= this.nurse.head();
		this.pilot= this.nurse.pilot();
	}
	
	public void run()
	{		
		while(running){
			//nurse detecte un obstacle
			if(this.head.detection(10)==true)
			{
				this.nurse.interruptTravel(); //interruption de l'itin�raire
				this.nurse.stop(); //nurse s'arrete
				
				//si pas d'obstacle � droite, contourne par la droite
				if(this.head.obstacleToTheRight(25)==false){ 
					this.nurse.bypassesByTheRight(15); 
				}
				//sinon si par d'obstacle � gauche, contourne par la gauche
				else if(this.head.obstacleToTheLeft(25)==false)	{
					this.nurse.bypassesByTheLeft(15); 
				}
				//sinon recule
				else {
					this.nurse.moveBackwardInScanning();
				}
				
				//nurse reprend son itin�raire
				this.nurse.resumeTravel(); 
				
			}
			//nurse d�tecte un choc, il interrompt son itin�raire et recule
			if(this.pilot.shockDetection())
			{
				this.nurse.interruptTravel();
				this.nurse.moveBackwardInScanning();
				this.nurse.resumeTravel();
			}	
		}		
	}
	
	public void stopThread()
	{
		running = false;
	}
	
}